<?php

namespace App\Http\Services;

use Exception;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Model\Mate;
use App\Http\Model\User;
use App\Http\Model\FinanceDiamond;
use App\Http\Model\FinanceUserWallet;
use App\Http\Model\Hint;

class MateServices extends CommonServices
{
    protected $mateModel;
    protected $userModel;
    protected $financeDiamondModel;
    protected $financeUserWalletModel;

    public function __construct()
    {
        parent::__construct();

        $this->mateModel = new Mate();
        $this->userModel = new User();
        $this->financeDiamondModel = new FinanceDiamond();
        $this->financeUserWalletModel = new FinanceUserWallet();

    }

    /**
     * 获取用户今日剩余匹配次数
     */
    public function getUserMateNum($userId){
        //免费次数
        $mateUserDayNum = $this->mateModel->mateUserDayNum;
        //用户每日已购买次数
        $mateUserDayBuyNum = $this->mateModel->getMateUserDayBuyNum($userId);
        //用户每日已使用匹配次数
        $mateUserDayUseNum = $this->mateModel->getMateUserDayUseNum($userId);

        $now = $mateUserDayNum + $mateUserDayBuyNum - $mateUserDayUseNum;
        return $now;
    }

    /**
     * 钻石兑换缘分配对次数
     */
    public function buyMateNum($userId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            //缘分配对次数兑换需要钻石
            $mateNeedDiamondNum = $this->mateModel->mateNeedDiamondNum;
            //用户余额
            $diamondNow = $this->financeUserWalletModel->getUserWalletDiamond($userId);
            if($diamondNow < $mateNeedDiamondNum) {
                $code = Hint::DIAMOND_NOT_ENOUGH;
                break;
            }

            DB::beginTransaction();
            try {
                $config = config('pay');
                //钻石
                $diamond_desc = $config['wallet_change_desc']['diamond_mate_buy'];
                $this->financeDiamondModel->inc($userId, 0, 0, 71, 0-$mateNeedDiamondNum, $diamond_desc);

                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();

                write_log('error_finance', 'buyMateNum', ['msg'=>$e->getMessage(), 'line'=>$e->getLine()]);
                $code = Hint::ERR_FAIL;
                break;
            }

            //增加次数
            $this->mateModel->incrMateUserDayBuyNum($userId, 1);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 消耗次数获取情话
     */
    public function getLoveTalk($userId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            //免费次数
            $mateUserDayNum = $this->mateModel->mateUserDayNum;
            //用户每日已购买次数
            $mateUserDayBuyNum = $this->mateModel->getMateUserDayBuyNum($userId);
            //用户每日已使用匹配次数
            $mateUserDayUseNum = $this->mateModel->getMateUserDayUseNum($userId);

            $now = $mateUserDayNum + $mateUserDayBuyNum - $mateUserDayUseNum;
            if($now < 1) {
                $code = Hint::DIAMOND_NOT_ENOUGH;
                $msg = "匹配次数不足";
                break;
            }

            //增加次数
            $this->mateModel->incrMateUserDayUseNum($userId, 1);

            //获取情话
            $data['talkId'] = 0;
            $data['talkContent'] = '';
            $list = $this->mateModel->getLoveTalk();
            if(!empty($list)) {
                $data['talkId'] = array_rand($list);
                $data['talkContent'] = $list[$data['talkId']]['content'];
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 获取匹配主播
     */
    public function getLoveUser($userId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            //可匹配主播
            $userList = $this->mateModel->getLoveUser();
            $loveUser = $userList[array_rand($userList)];
            //爱情箴言
            $mottoList = $this->mateModel->getLoveMotto();
            $motto = $mottoList[array_rand($mottoList)];
            if(empty($loveUser) || empty($motto)) {
                $code = Hint::ERROR;
                break;
            }
            $userInfo = $this->userModel->get_user($loveUser['user_id']);
            if(empty($userInfo)) {
                $code = Hint::ERROR;
                break;
            }

            $data['user_id'] = $loveUser['user_id'];
            $data['user_uid'] = $userInfo['user_uid'];
            $data['nickname'] = $userInfo['nickname'];
            $data['gender'] = $userInfo['gender'];
            $data['head_portrait'] = $userInfo['images'] ? $userInfo['images'][0] : '';
            $data['voice'] = build_url($loveUser['voice']);
            $data['motto'] = $motto['content'];
            $data['score'] = mt_rand(85, 95);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 获取语音通话配置
     */
    public function voiceCallConfig($userId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            $data['voiceCallNeedNum'] = $this->mateModel->voiceCallNeedNum;

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 支付语音通话费用
     */
    public function payVoiceCall($userId, $callUserId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];

        do {
            //语音通话每分钟消耗钻石数
            $voiceCallNeedNum = $this->mateModel->voiceCallNeedNum;
            //用户余额
            $diamondNow = $this->financeUserWalletModel->getUserWalletDiamond($userId);
            if($diamondNow < $voiceCallNeedNum) {
                $code = Hint::DIAMOND_NOT_ENOUGH;
                break;
            }

            DB::beginTransaction();
            try {
                $config = config('pay');
                //钻石
                $diamond_desc = $config['wallet_change_desc']['diamond_mate_voice_call'];
                $this->financeDiamondModel->inc($userId, $callUserId, 0, 72, 0-$voiceCallNeedNum, $diamond_desc);

                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();

                write_log('error_finance', 'buyMateNum', ['msg'=>$e->getMessage(), 'line'=>$e->getLine()]);
                $code = Hint::ERR_FAIL;
                break;
            }

            //用户余额
            $diamondNow = $this->financeUserWalletModel->getUserWalletDiamond($userId);
            $data['diamond_balance'] = $diamondNow;

        } while (false);

        return compact('code', 'msg', 'data');
    }
}
